/*
 * srm 207 div 2 prob 3
 */
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;


public class CaptureThemAll
{
	int getData (String s) {
		int x = s.charAt (0) - 'a', y = s.charAt (1) - '1';
		return 8 * x + y;
	}
	
	static int[][] dir = {
		{-2, -1}, {-2, 1}, {2, -1}, {2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}
	};
	
	int getNext (int d, int i) {
		int x = d/8, y = d%8;
		x += dir[i][0];
		y += dir[i][1];
		if (x < 0 || x > 7 || y < 0 || y > 7)
			return -1;
		return 8 * x + y;
	}
	
	int fastKnight1 (String a, String b) {
		int d1 = getData (a), d2 = getData (b);
		HashSet<Integer> visited = new HashSet<Integer> ();
		LinkedList<Integer> queueD = new LinkedList<Integer> (), queueL = new LinkedList<Integer> ();
		
		queueD.addLast (d1);
		queueL.addLast (0);
		visited.add (d1);
		while (!queueD.isEmpty ()) {
			int d = queueD.removeFirst (), l = queueL.removeFirst ();
			if (d == d2) 
				return l;
			for (int i = 0; i < 8; ++i) {
				int k = getNext (d, i);
				if (k != -1 && !visited.contains (k)) {
					queueD.addLast (k);
					visited.add (k);
					queueL.addLast (l+1);
				}
			}
		}
		return -1;
	} 

	public int fastKnight(String knight, String rook, String queen)
	{
		int k1 = fastKnight1 (knight, rook), k2 = fastKnight1 (knight, queen), k3 = fastKnight1 (rook, queen);
		if (k1 < k2)
			return k1 + k3;
		return k2 + k3;
	}
	

}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
